
function HTMLEncode(html) {
    var temp = document.createElement("div");
    (temp.textContent != null) ? (temp.textContent = html) : (temp.innerText = html);
    var output = temp.innerHTML;
    temp = null;
    return output;
}

function HTMLDecode(text) {
    var temp = document.createElement("div");
    temp.innerHTML = text;
    var output = temp.innerText || temp.textContent;
    temp = null;
    return output;
}
function initSelectByJObject(jOb, jDom) {
    $.each(jOb, function (i, item) {
        $("<option value='"+item.Key+"'>"+item.Text+"</option>").appendTo(jDom);
    });
}
function initNullCid() {
    $("#ddlCompany").empty();
    $("#hdCompany").val("");
    $("#hdCompanyName").val("");
    if (usertypeid == 'YHLX0101' || usertypeid == 'YHLX0102') {
        var item = [{ Key: "", Text: "请选择" }];
        initSelectByJObject(item, $("#ddlCompany"));
    }
}
function initNullGid() {
    $("#ddlGranary").empty();
    $("#hdGranary").val("");
    $("#hdGranaryName").val("");
    if (usertypeid == 'YHLX0101' || usertypeid == 'YHLX0102' || usertypeid == 'YHLX0103') {
        if ($("#hdGranaryAll").val() == "1") {
            var item = [{ Key: "", Text: "全部" }];
            initSelectByJObject(item, $("#ddlGranary"));
        } else {
            var item = [{ Key: "", Text: "请选择" }];
            initSelectByJObject(item, $("#ddlGranary"));
        }
    }
}
function initNullDcid() {
    $("#ddlGranaryDC").empty();
    $("#hdGranaryDC").val("");
    $("#hdGranaryDCName").val("");
    if (usertypeid == 'YHLX0101' || usertypeid == 'YHLX0102' || usertypeid == 'YHLX0103' || usertypeid == 'YHLX0104') {
        if ($("#hdGranaryDCAll").val() == "1") {
            var item = [{ Key: "", Text: "全部" }];
            initSelectByJObject(item, $("#ddlGranaryDC"));
        } else {
            var item = [{ Key: "", Text: "请选择" }];
            initSelectByJObject(item, $("#ddlGranaryDC"));
        }
    }
}
function initNullHid() {
    $("#ddlStoreHouse").empty();
    $("#hdStoreHouseID").val("");
    $("#hdStoreHouseName").val("");
    if ($("#hdHouseAll").val() == "1") {
        var item = [{ Key: "", Text: "全部" }];
        initSelectByJObject(item, $("#ddlStoreHouse"));
    } else {
        var item = [{ Key: "", Text: "请选择" }];
        initSelectByJObject(item, $("#ddlStoreHouse"));
    }
}
function setCid(jOb, jDom) {
    initNullCid();
    initSelectByJObject(jOb, jDom);
    SaveOrgDom();
}
function setGid(jOb, jDom) {
    initNullGid();
    initSelectByJObject(jOb, jDom);
    SaveOrgDom();
}
function setDcid(jOb, jDom) {
    var change = false;
    if (jOb.length == 1) {
        $("#ddlGranaryDC").empty();
        $("#hdGranaryDC").val("");
        $("#hdGranaryDCName").val("");
        change = true;
    } else {
        initNullDcid();
    }
    initSelectByJObject(jOb, jDom);
    $("#ddlGranaryDC").change();
    SaveOrgDom();
}
function setHid(jOb, jDom) {
    initNullHid();
    initSelectByJObject(jOb, jDom);
    SaveOrgDom();
}
function SaveOrgDom() {
    var json = {};
    json.cids = $("#ddlCompany").html();
    json.cidsSelect = $("#ddlCompany").val();
    json.gids = $("#ddlGranary").html();
    json.gidsSelect = $("#ddlGranary").val();
    json.dcids = $("#ddlGranaryDC").html();
    json.dcidsSelect = $("#ddlGranaryDC").val();
    json.hids = $("#ddlStoreHouse").html();
    json.hidsSelect = $("#ddlStoreHouse").val();
    $("#hdOrgsInfo").val(HTMLEncode(JSON.stringify(json)));
}
function initLoadOrg() {
    $.ajax({
        async: false,
        type: "post",
        url: "../../Data/LoadGranaryHouse.ashx?func=Init",//页面加载绑定
        dataType: "text",
        success: function (result) {
            if (result.length > 0) {
                var json = eval("(" + result + ")");
                if (json.cids != undefined) {
                    setCid(json.cids, $("#ddlCompany"));
                }
                if (json.gids != undefined) {
                    setGid(json.gids, $("#ddlGranary"));
                }
                if (json.dcids != undefined) {
                    setDcid(json.dcids, $("#ddlGranaryDC"));
                }
                if (json.hids != undefined) {
                    setHid(json.hids, $("#ddlStoreHouse"));
                }
            }
        }
    });
}
$(function () {
    $("#ddlCompany").change(function () {
        SaveOrgDom();
        var cid = $("#ddlCompany").val();
        $("#hdCompany").val($("#ddlCompany").val());
        $("#hdCompanyName").val($("#ddlCompany").find("option:selected").text());
        initNullGid();
        initNullDcid();
        initNullHid();
        if (cid == "")
            return;
        $.ajax({
            async: false,
            type: "post",
            url: "../../Data/LoadGranaryHouse.ashx?func=LoadGidsByCid",//页面加载绑定
            data: { cid: cid },
            dataType: "text",
            success: function (result) {
                if (result.length > 0) {
                    var json = eval("(" + result + ")");
                    if (json.cids != undefined) {
                        setCid(json.cids, $("#ddlCompany"));
                    }
                    if (json.gids != undefined) {
                        setGid(json.gids, $("#ddlGranary"));
                    }
                    if (json.dcids != undefined) {
                        setDcid(json.dcids, $("#ddlGranaryDC"));
                    }
                    if (json.hids != undefined) {
                        setHid(json.hids, $("#ddlStoreHouse"));
                    }
                }
            }
        });
    });
    $("#ddlGranary").change(function () {
        SaveOrgDom();
        var gid = $("#ddlGranary").val();
        $("#hdGranary").val($("#ddlGranary").val());
        $("#hdGranaryName").val($("#ddlGranary").find("option:selected").text());
        initNullDcid();
        initNullHid();
        if (gid == "")
            return;
        $.ajax({
            async: false,
            type: "post",
            url: "../../Data/LoadGranaryHouse.ashx?func=LoadDcidsByGid",//页面加载绑定
            data: { gid: gid },
            dataType: "text",
            success: function (result) {
                if (result.length > 0) {
                    var json = eval("(" + result + ")");
                    if (json.cids != undefined) {
                        setCid(json.cids, $("#ddlCompany"));
                    }
                    if (json.gids != undefined) {
                        setGid(json.gids, $("#ddlGranary"));
                    }
                    if (json.dcids != undefined) {
                        setDcid(json.dcids, $("#ddlGranaryDC"));
                    }
                    if (json.hids != undefined) {
                        setHid(json.hids, $("#ddlStoreHouse"));
                    }
                }
            }
        });
    });
    $("#ddlGranaryDC").change(function () {
        SaveOrgDom();
        var orgcode = $("#ddlGranaryDC").val();
        $("#hdGranaryDC").val($("#ddlGranaryDC").val());
        $("#hdGranaryDCName").val($("#ddlGranaryDC").find("option:selected").text());
        //if (orgcode == "") {
        //    orgcode=$("#ddlGranary").val();
        //}
        initNullHid();
        if (orgcode == "")
            return;
        $.ajax({
            async: false,
            type: "post",
            url: "../../Data/LoadGranaryHouse.ashx?func=LoadHouseListByOrgCode",//页面加载绑定
            data: { orgcode: orgcode },
            dataType: "text",
            success: function (result) {
                if (result.length > 0) {
                    var json = eval("(" + result + ")");
                    if (json.cids != undefined) {
                        setCid(json.cids, $("#ddlCompany"));
                    }
                    if (json.gids != undefined) {
                        setGid(json.gids, $("#ddlGranary"));
                    }
                    if (json.dcids != undefined) {
                        setDcid(json.dcids, $("#ddlGranaryDC"));
                    }
                    if (json.hids != undefined) {
                        setHid(json.hids, $("#ddlStoreHouse"));
                    }
                }
            }
        });
    });
    $("#ddlStoreHouse").change(function () {
        SaveOrgDom();
        var hid = $("#ddlStoreHouse").val();
        $("#hdStoreHouseID").val($("#ddlStoreHouse").val());
        $("#hdStoreHouseName").val($("#ddlStoreHouse").find("option:selected").text());
    });
    
    if ($("#hdOrgsInfo").val() == "") {
        initLoadOrg();
    } else {
        var hisStr = HTMLDecode($("#hdOrgsInfo").val());
        if (hisStr == "" || hisStr == undefined) {
            initLoadOrg();
        } else {
            var json = eval("(" + hisStr + ")");
            if (json.cids != undefined) {
                $("#ddlCompany").html(json.cids);
            }
            if (json.cidsSelect != undefined) {
                $("#ddlCompany").val(json.cidsSelect);
            }
            if (json.gids != undefined) {
                $("#ddlGranary").html(json.gids);
            }
            if (json.gidsSelect != undefined) {
                $("#ddlGranary").val(json.gidsSelect);
            }
            if (json.dcids != undefined) {
                $("#ddlGranaryDC").html(json.dcids);
            }
            if (json.dcidsSelect != undefined) {
                $("#ddlGranaryDC").val(json.dcidsSelect);
            }
            if (json.hids != undefined) {
                $("#ddlStoreHouse").html(json.hids);
            }
            if (json.hidsSelect != undefined) {
                $("#ddlStoreHouse").val(json.hidsSelect);
            }
        }
    }
});